﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using FacebookXmpp;
using XDAFacebook;
using XFControls;
using XFControls.XFPanels;
using XFControls.XFPanels.XFPanelItems;
using XFItemControls.XFPanels.XFPanelItems;
using XFSenseUI.XFItems;

namespace XFItemControls
{
    public class XFChat
    {
        public static Dashboard Dashboard;


        public static XFPanelList GetChatPanel()
        {
            var panel = new XFPanelList();
            panel.Disposed += (o, e) =>
                                  {
                                      if ((Dashboard == null) || (Dashboard.IsAuthenticated))
                                          return;

                                      if (Dashboard != null)
                                          Dashboard.Dispose();
                                      Dashboard = null;
                                  };


            var login = new XFWorkerChatLogin();
            var loading = new XFItemLoading(login);

            loading.WorkFinished += loading_WorkFinished;

            panel.Add(loading);
            return panel;
        }

        static void loading_WorkFinished(object sender, WorkerEventArgs e)
        {
            var loading = (XFItemLoading)sender;
            var panel = (XFPanelList)loading.Parent;

            if (panel.InvokeRequired)
            {
                panel.Invoke(new Action(() => loading_WorkFinished(sender, e)));
                return;
            }

            if (!Dashboard.IsAuthenticated)
            {
                RetryAuthentication(panel);
                return;
            }

            var tabs = new XFItemThreeTabs
            {
                TabOne = Settings.LocalizationStrings.Online,
                TabTwo = Settings.LocalizationStrings.Chat,
                TabThree = Settings.LocalizationStrings.Status
            };

            tabs.OneClick += Tabs_OneClick;
            tabs.TwoClick += Tabs_TwoClick;
            tabs.ThreeClick += Tabs_ThreeClick;

            panel.Add(tabs);

            Tabs_OneClick(null, new XFPanelListEventArgs(panel));


            //var tb = new SenseItemTextBox { SendButton = true };
            //tb.SetHeight(100);
            //panel.Add(tb);
        }

        private static void RetryAuthentication(XFPanelList panel)
        {
            var button = new SenseItem();
            button.MainText = "Login Failed";

            //button.Click += (o, e) => ((XFPanelContainer)panel.Parent).SetMainPanel(GetChatPanel());

            panel.Add(button);
        }

        private static void Tabs_OneClick(object sender, XFPanelListEventArgs e)
        {
            var panel = e.List;

            Dashboard.UpdateFriends();
            SenseItemDivider devider = null;
            foreach (var group in Dashboard.Groups)
            {
                devider = new SenseItemDivider();
                devider.Text = group;
                panel.Add(devider);
                foreach (var friend in Dashboard.Friends.Where(friend => friend.State == Status.Available && friend.Groups.Contains(group)))
                {
                    panel.Add(new XFItemChatFriend(friend));
                }

                foreach (var friend in Dashboard.Friends.Where(friend => friend.State == Status.Away && friend.Groups.Contains(group)))
                {
                    panel.Add(new XFItemChatFriend(friend));
                }
            }
            devider = new SenseItemDivider();
            devider.Text = "Default Group";
            panel.Add(devider);
            foreach (var friend in Dashboard.Friends.Where(friend => friend.State == Status.Available && friend.Groups.Count == 0))
            {
                panel.Add(new XFItemChatFriend(friend));
            }
            foreach (var friend in Dashboard.Friends.Where(friend => friend.State == Status.Away && friend.Groups.Count == 0))
            {
                panel.Add(new XFItemChatFriend(friend));
            }
        }

        private static void Tabs_TwoClick(object sender, XFPanelListEventArgs e)
        {
        }

        private static void Tabs_ThreeClick(object sender, XFPanelListEventArgs e)
        {
        }
    }
}
